package com.example.bookmall.controller.adminSystem;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.bookmall.entity.Aftersales;
import com.example.bookmall.entity.Result;
import com.example.bookmall.entity.order.OrderDTO;
import com.example.bookmall.entity.order.OrderVo;
import com.example.bookmall.entity.order.Orders;
import com.example.bookmall.entity.usercore.User;
import com.example.bookmall.service.OrderService;
import com.example.bookmall.service.UserService;
import com.example.bookmall.utils.ThreadLocalUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/adminOrder")
public class AdminOrderController {

    @Autowired
    private OrderService orderService;
    @Autowired
    private UserService userService;

    //获取所有订单
    //根据状态status 和 订单id->keyword
    //如果管理员需要 获取售后的订单 status为6即未售后
    @GetMapping("/list")
    public Result getAllOrders(
            @RequestParam(defaultValue = "1") Long pageNum,
            @RequestParam(defaultValue = "10") Long pageSize,
            String keyword,
            Integer status
    ) {
        Map<String, Object> map = ThreadLocalUtil.get();
        Integer userId = (Integer) map.get("userId");
        User currentuser = userService.findById(userId);

        if (currentuser.getRole() == 1) {
            return Result.error("权限不足");
        }
        Page<OrderVo> page = new Page<>(pageNum, pageSize);
        IPage<OrderVo> orderPage = orderService.getOrderVoPage(page, keyword, status);
        return Result.success(orderPage);
    }




    //修改订单状态
    @GetMapping("/UpdateOrderStatus")
    public  Result UpdateOrderStatus(String  orderId,Integer status){
        Map<String, Object> map = ThreadLocalUtil.get();
        Integer userId = (Integer) map.get("userId");
        User currentuser = userService.findById(userId);

        if (currentuser.getRole() == 1) {
            return Result.error("权限不足");
        }
        return orderService.UpdateOrderStatus(orderId,status);
    }


    //修改订单地址
    @PostMapping("/UpdateOrderAddress")
    public  Result UpdateOrderAddress(@RequestBody OrderDTO orderDTO){
        Map<String, Object> map = ThreadLocalUtil.get();
        Integer userId = (Integer) map.get("userId");
        User currentuser = userService.findById(userId);

        if (currentuser.getRole() == 1) {
            return Result.error("权限不足");
        }
        return orderService.UpdateOrderAddress(orderDTO);
    }



    //根据订单id删除订单信息
    @GetMapping("/deleteOrderById")
    public Result deleteOrderById(String orderId){
        Map<String, Object> map = ThreadLocalUtil.get();
        Integer userId = (Integer) map.get("userId");
        User currentuser = userService.findById(userId);

        if (currentuser.getRole() == 1) {
            return Result.error("权限不足");
        }
        return orderService.deleteOrderById(orderId);
    }


    //批量删除用户订单
    @PostMapping("/batchDeleteOrders")
    public Result batchDeleteOrders (@RequestBody List<String> orderIds){
        Map<String, Object> map = ThreadLocalUtil.get();
        Integer userId = (Integer) map.get("userId");
        User currentuser = userService.findById(userId);

        if (currentuser.getRole() == 1) {
            return Result.error("权限不足");
        }
        return  orderService.batchDeleteOrders(orderIds);
    }

    //根据订单id返回售后结果（若存在售后）
    @GetMapping("/getRetResult")
    public  Result getRetResult(String orderId){
        Map<String, Object> map = ThreadLocalUtil.get();
        Integer userId = (Integer) map.get("userId");
        User currentuser = userService.findById(userId);

        if (currentuser.getRole() == 1) {
            return Result.error("权限不足");
        }
        return orderService.getretuResult(orderId);
    }


    //处理售后
    @PostMapping("/UpdateRetResult")
    public  Result UpdateRetResult(@RequestBody Aftersales aftersales){
        Map<String, Object> map = ThreadLocalUtil.get();
        Integer userId = (Integer) map.get("userId");
        User currentuser = userService.findById(userId);

        if (currentuser.getRole() == 1) {
            return Result.error("权限不足");
        }
        return orderService.UpdateRetResult(aftersales);
    }

}
